/**
Game.cpp
Title: Binge Game Program File
Descr: This provides the Framework for a Binge Game.

Copyright (c) 2010, Brant Ryan Anderson
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the <ORGANIZATION> nor the names of its contributors
      may be used to endorse or promote products derived from this software
      without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**/

#include "../binge/Game.hpp"

namespace binge
{

Game::Game(void)
{
	// Create the engine.
	engine = new binge::Engine();

	std::ostringstream s;
	s << "Copyright (c) 2010, Brant Ryan Anderson";
	s << std::endl;
	s << "All rights reserved.";
	s << std::endl << std::endl;
	s << "Redistribution and use in source and binary forms, with or without ";
	s << "modification, are permitted provided that the following conditions are met:";
	s << std::endl << std::endl;
    s << "* Redistributions of source code must retain the above copyright notice, ";
	s << "this list of conditions and the following disclaimer.";
	s << std::endl;
    s << "* Redistributions in binary form must reproduce the above copyright ";
	s << "notice, this list of conditions and the following disclaimer in the ";
	s << "documentation and/or other materials provided with the distribution.";
	s << std::endl;
    s << "* Neither the name of the Binge Creators nor the names of its contributors ";
	s << "may be used to endorse or promote products derived from this software ";
	s << "without specific prior written permission.";
	s << std::endl << std::endl;
	s << "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" ";
	s << "AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE ";
	s << "IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE ";
	s << "DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE ";
	s << "FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL ";
	s << "DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR ";
	s << "SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER ";
	s << "CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, ";
	s << "OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ";
	s << "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.";
	bsdLicense = s.str();
}

Game::~Game(void)
{
	delete engine;
	engine = 0;
}


void // The required License for binary form.
Game::showBSDLicense(void)
{
	engine->Message(bsdLicense.c_str(), "3-clause BSD License");
}

int
Game::gameMain()
{
	srand((unsigned int)time(NULL));

	//preload any settings or misc stuff
	if (!gamePreload()) {
		engine->Fatal_Error("An error occurred in gamePreload().", "Fatal Error");

		return EXIT_FAILURE;
	}
	//initialize the engine
	if (!engine->Init()) 	{
		engine->Fatal_Error("An error occurred while initializing the engine in engine->Init().", "Fatal Error");

		return EXIT_FAILURE;
	}
	//initialize the game
	if (!gameInit()) {
		engine->Fatal_Error("An error occurred in gameInit().", "Fatal Error");

		return EXIT_FAILURE;
	}

	// main message loop
	sf::Event event;
	engine->gameover = false;
	while (!engine->gameover)
	{
		//While there are events to handle
        while (engine->screen.GetEvent(event))
        {
            // Close window : exit
            if (event.Type == sf::Event::Closed)
                engine->gameover = true;

            //Handle user input
            gameHandleInput(event);
        }

		engine->Update(	&BaseGame::gameUpdate,
						&BaseGame::gameRender,
						this);
	}
   	engine->Close(&BaseGame::gameEnd, this);
   	delete engine;
   	engine = 0;

	return EXIT_SUCCESS;
}

} // namespace
